//------------------------------------------------------------
// * Author         : Ekko
// * Description    : 滑动列表
//------------------------------------------------------------

export type ScrollItemTemplate = {
    key: string;
    node: cc.Node;
}

export type ScrollItemData = {
    key: string;
    data: any;
}

export enum ScrollDirection{
    Vertical = 1,
    Horizontal = 2,
}

type ScrollViewParams = {
    scrollview: cc.ScrollView;
    mask: cc.Node;
    content: cc.Node;
    item_templates: ScrollItemTemplate[];
    direction?: ScrollDirection;
    width?: number;
    height?: number;
    gapX?: number;
    gapY?: number;
    cb_host?: any;                                                                                  // 回调函数host
    item_setter: (item: cc.Node, key: string, data: any, index: number) => [number, number];        // item更新setter 
    recycle_cb?: (item: cc.Node, key: string) => void;                                              // 回收时的回调
    scroll_to_end_cb?: () => void;                                                                  // 滚动到尽头的回调
    auto_scrolling?: boolean;                                                                       // append时自动滚动到尽头
}

type ScrollItem = {
    x: number;
    y: number;
    width: number;
    height: number;
    data: ScrollItemData;
    node: cc.Node;
}

export class ScrollView{
    private scrollview: cc.ScrollView;
    private mask: cc.Node;
    private content: cc.Node;
    private item_templates: Map<string, cc.Node>;
    private node_pools: Map<string, cc.Node[]>;

    private dir: number;
    private width: number;
    private height: number;
    private gapX: number;
    private gapY: number;
    private cb_host: any;
    private item_setter: (item: cc.Node, key: string, data: any, index: number) => [number, number];
    private recycle_cb: (item: cc.Node, key: string) => void;
    private scroll_to_end_cb: () => void;
    private auto_scrolling: boolean;
    private items: ScrollItem[];
    private start_index: number;
    private stop_index: number;

    constructor(params: ScrollViewParams){
        this.scrollview = params.scrollview;
        this.mask = params.mask;
        this.content = params.content;
        this.node_pools = new Map();
        this.item_templates = new Map();
        params.item_templates.forEach((tpl) => {
            tpl.node.active = false;
            this.item_templates.set(tpl.key, tpl.node);
        });
    }
}